package a66weding.com.jiehuntong.activity.toolsseat;

import android.content.Context;
import android.os.Bundle;
import android.os.IBinder;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.PopupWindow;

import a66weding.com.jiehuntong.R;
import a66weding.com.jiehuntong.application.DataWareHourse;
import a66weding.com.jiehuntong.beans.SeatBean;
import a66weding.com.jiehuntong.beans.User;
import me.imid.swipebacklayout.lib.app.SwipeBackActivity;

import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;

/**
 * @version :
 * @creator : AxDu
 * @create-time : 2016/6/27
 * @description :
 */
public class SeatBaseActivity extends SwipeBackActivity {

     User user;

    public PopupWindow mCommitpop;
    private View noNetView;
    private SeatBean.DataBean mSeatbean;
    protected static final int HAVEDATA=1;
    protected static final int NODATA=0;
    protected static final int DATAERROR=2;


    public SeatBaseActivity(){



    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);

        user = DataWareHourse.getInstance().getmUser();

    }

    protected void initdata(){

    }




    public void showCommitPopupWindow(Context context, View view) {
        // 一个自定义的布局，作为显示的内容
        View contentView = LayoutInflater.from(context).inflate(
                R.layout.pop_tools_wedregister_commit, null);
        mCommitpop = new PopupWindow(contentView,
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, true);

        //点击空白处时，隐藏掉pop窗口
        mCommitpop.setTouchable(true);
        contentView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mCommitpop.dismiss();
            }
        });
        mCommitpop.setTouchInterceptor(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return true;
            }
        });
        //设置位置
        mCommitpop.showAtLocation(view, Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL,0,0);
        // 设置好参数之后再show
        mCommitpop.showAsDropDown(view);

    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            View view = getCurrentFocus();
            if (isHideInput(view, ev)) {
                HideSoftInput(view.getWindowToken());
            }
        }
        return super.dispatchTouchEvent(ev);
    }
    // 判定是否需要隐藏
    private boolean isHideInput(View v, MotionEvent ev) {
        if (v != null && (v instanceof EditText)) {
            int[] l = { 0, 0 };
            v.getLocationInWindow(l);
            int left = l[0], top = l[1], bottom = top + v.getHeight(), right = left
                    + v.getWidth();
            if (ev.getX() > left && ev.getX() < right && ev.getY() > top
                    && ev.getY() < bottom) {
                return false;
            } else {
                return true;
            }
        }
        return false;
    }
    // 隐藏软键盘
    private void HideSoftInput(IBinder token) {
        if (token != null) {
            InputMethodManager manager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            manager.hideSoftInputFromWindow(token,
                    InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }


    public void loadNoNetWorkView(Context context,ViewGroup view){
        noNetView = View.inflate(context, R.layout.icon_network_no, null);
        ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(MATCH_PARENT, MATCH_PARENT);
        noNetView.setLayoutParams(layoutParams);

        view.addView(noNetView);



    }

    public void removeNoNetWorkView(Context context,ViewGroup view){
        if(noNetView!=null) {
         view.removeView(noNetView);
        }

    }





    public SeatBean.DataBean getmSeatbean() {
        return mSeatbean;
    }

    public void setmSeatbean(SeatBean.DataBean mSeatbean) {
        this.mSeatbean = mSeatbean;
    }
}
